/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.lestr.udm.core2;

import java.io.InputStream;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author pibonnin
 */
public class DefaultDataManager<S extends IDataManager.ISession, P extends IDataManager.IParameters> implements IDataManager<P> {

    private P parameters;

    public DefaultDataManager() {
    }

    @Override
    public ISession openSession() throws GdmException {

        Type type = getClass().getGenericSuperclass();
        ParameterizedType paramType = (ParameterizedType) type;
        Class<S> classe = (Class<S>) paramType.getActualTypeArguments()[0];

        try {
            return classe.newInstance();
        } catch (InstantiationException ex) {
            Logger.getLogger(DefaultDataManager.class.getName()).log(Level.SEVERE, null, ex);
            throw new GdmException(ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(DefaultDataManager.class.getName()).log(Level.SEVERE, null, ex);
            throw new GdmException(ex);
        }

    }

    public P getParameters() {

        if (parameters == null) {

            Type type = getClass().getGenericSuperclass();
            ParameterizedType paramType = (ParameterizedType) type;
            Class<P> classe = (Class<P>) paramType.getActualTypeArguments()[1];

            try {
                parameters = classe.newInstance();
            } catch (InstantiationException ex) {
                Logger.getLogger(DefaultDataManager.class.getName()).log(Level.SEVERE, null, ex);
                throw new RuntimeException(ex);
            } catch (IllegalAccessException ex) {
                Logger.getLogger(DefaultDataManager.class.getName()).log(Level.SEVERE, null, ex);
                throw new RuntimeException(ex);
            }

        }
        
        return parameters;

    }

    public static class DefaultSession implements IDataManager.ISession {

        @Override
        public boolean addData(String id) throws GdmException {
            return false;
        }

        @Override
        public boolean existsData(String id) throws GdmException {
            return false;
        }

        @Override
        public boolean removeData(String id) throws GdmException {
            return false;
        }

        @Override
        public boolean setDataValue(String id,
                InputStream value) throws GdmException {
            return false;
        }

        @Override
        public InputStream getDataValue(String id) throws GdmException {
            return null;
        }

        @Override
        public long getDataLength(String id) throws GdmException {
            return 0;
        }

        @Override
        public boolean commit() throws GdmException {
            return true;
        }

        @Override
        public boolean close() throws GdmException {
            return true;
        }
    }

    public static class DefaultParameters implements IDataManager.IParameters {
    }
}// END Class AbstractDataManager
